<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/static/admin/jsp/include.jsp"%>
    <style type="text/css">
        .textbox.textbox-focused .textbox-addon,
        .textbox.textbox-focused .textbox-text{
            background: #eee!important;
        }
    </style>
    <script type="text/javascript">
        var id = "${id}";
        submitForm = function($dialog, $grid, $pjq) {
            if ($('form').form('validate')) {
                var obj=sy.serializeObject($('form'));
                var url=sy.contextPath + '/chemicalType/save';
                var data = [];
                for (var i = 0; i <= resultJson; i++){
                    if ($('#content'+i).length == 0) continue;
                    var title = $('#title'+i).val();
                    var content = $('#content'+i).val();
                    if (!title&&!content){
                        continue;
                    }

                    var json = {title:title,content:content};
                    data.push(json);
                }
                obj.rangUpInfo = sy.jsonToString(data);
                obj.id = id;
                parent.$.messager.confirm('询问', '确定保存？', function(r) {
                    if (r) {
                        $.post(url, obj, function(result) {
                            if (result.code == 0) {
                                parent.$.messager.i('保存成功');
                                $grid.datagrid('reload');
                                $dialog.dialog('destroy');
                            } else {
                                $pjq.messager.e('保存失败,'+result.msg);
                            }
                        }, 'json');
                    }
                });
            }
        };

        $(function() {
            if(id != ''){
                $.post(sy.contextPath + '/chemicalType/findById', {
                    id : id,
                }, function(result) {
                    if (id != '') {
//                        parent.$.messager.progress({
//                            text : '数据加载中....'
//                        });
                        $.post(sy.contextPath + '/chemicalType/findById', {
                            id : id
                        }, function(result) {
                            if(result.rangUpInfo != ''){
                                var data = sy.stringToJSON(result.rangUpInfo);
                                console.log(data);
                                for (var i = 0; i< data.length; i++){
                                    add();
                                    $('#title'+i).textbox('setValue',data[i].title);
                                    $('#content'+i).textbox('setValue',data[i].content);
                                }
                            }
                            parent.$.messager.progress('close');
                        }, 'json');

                    }
                })
            }
        });

        var resultJson = 0;
        function add(){
            if(resultJson == 2){
                return layer.msg("当前支持2级");
            }

            var htmls = '';

            htmls +=
                '<tr id="titles'+resultJson+'" >' +
                '<th style="width:30px;">'+('标题')+'</th>'+
                '<td>'+
                '<input id="title'+resultJson+'" name="title'+resultJson+'" class="easyui-textbox" data-options="validType:['+'length[0,300]'+'],multiline : true" missingMessage="请输入'+('标题')+'"/>'+
                '</td>'+
                '</tr>'+
                '<tr id="contents'+resultJson+'">' +
                '<th style="width:30px;">'+('内容')+'</th>'+
                '<td>'+
                '<input id="content'+resultJson+'" name="content'+resultJson+'" class="easyui-textbox" data-options="validType:['+'length[0,300]'+'],multiline : true" missingMessage="请输入'+('内容')+'"/>'+
                '</td>'+
                '</tr>';

            $("#resultJson").append(htmls);
            $.parser.parse('#titles'+resultJson);
            $.parser.parse('#contents'+resultJson);
            resultJson++;
        }

        function delTuwen(index){
            $('#titles'+index).remove();
            $('#contents'+index).remove();
            resultJson--;
        }

    </script>
</head>
<body>
<form id="form" method="post">
    <input name="id" type="hidden" />
    <div style="padding:15px;font-size: 12px">
        <table style="table-layout:fixed;" border="0" cellspacing="0" class="formtable">
            <tr >
                <th style="width: 100px">结果分析</th>
                <td colspan="3">
                    <div id="addBtn"><a href="javascript:void(0);" class="easyui-linkbutton" onclick="add()">添加</a></div>
                    <table style="table-layout:fixed;" border="0" cellspacing="0" class="formtable" id='resultJson'>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</form>
</body>
</html>